package de.piratech.dasding.data.database.mongo.data;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.vz.mongodb.jackson.DBRef;
import de.piratech.dasding.data.ActionApplication;
import de.piratech.dasding.data.ActionCategory;
import de.piratech.dasding.data.EventCategory;
import de.piratech.dasding.data.User;
import de.piratech.dasding.data.User.USER_ROLE;
import de.piratech.dasding.data.database.mongo.AbstractMongoDB.COLLECTIONS;

public class MongoUser extends AbstractMongoDataObject<User> {

  public static boolean querying;

  private List<DBRef<MongoActionCategory, String>> actionInterestRefs = new ArrayList<DBRef<MongoActionCategory, String>>();
  private List<DBRef<MongoEventCategory, String>> eventInterestRefs = new ArrayList<DBRef<MongoEventCategory, String>>();
  private List<MongoActionApplication> actionApplicationsRefs = new ArrayList<MongoActionApplication>();

  public MongoUser() {
    super(new User());
  }

  public MongoUser(User object) {
    super(object);
    if (object.getInterests() != null) {
      for (ActionCategory category : object.getInterests()) {
        actionInterestRefs.add(new DBRef<MongoActionCategory, String>(category
            .getId(), COLLECTIONS.ACTIONCATEGORY_COLLECTION.getName()));
      }
    }
    if (object.getInterestedIn() != null) {
      for (EventCategory category : object.getInterestedIn()) {
        eventInterestRefs.add(new DBRef<MongoEventCategory, String>(category
            .getId(), COLLECTIONS.EVENTCATEGORY_COLLECTION.getName()));
      }
    }
    if (object.getActionApplications() != null) {
      for (ActionApplication category : object.getActionApplications()) {
        actionApplicationsRefs.add(new MongoActionApplication(category));
      }
    }
  }

  public String getName() {
    return getDataObject().getName();
  }

  public void setName(String pName) {
    getDataObject().setName(pName);
  }

  public String getFullname() {
    return getDataObject().getFullname();
  }

  public void setFullname(String pFullname) {
    getDataObject().setFullname(pFullname);
  }

  /**
   * @return the encrypted password
   */
  public String getPassword() {
    return getDataObject().getPassword();
  }

  /**
   * sets the password (DOES NOT ENCRYPT IT!!!)
   * 
   * @param pPassword
   */
  public void setPassword(String pPassword) {
    getDataObject().setPassword(pPassword);
  }

  /**
   * @return phone number
   */
  public String getPhone() {
    return getDataObject().getPhone();
  }

  /**
   * Sets the phonenumber
   * 
   * @param pPhone
   */
  public void setPhone(String pPhone) {
    getDataObject().setPhone(pPhone);
  }

  /**
   * @return mobile number
   */
  public String getMobile() {
    return getDataObject().getMobile();
  }

  /**
   * Sets the mobile number
   * 
   * @param pMobile
   */
  public void setMobile(String pMobile) {
    getDataObject().setMobile(pMobile);
  }

  /**
   * @return mailadress
   */
  public String getMail() {
    return getDataObject().getMail();
  }

  /**
   * Sets the mail adress
   * 
   * @param pMail
   */
  public void setMail(String pMail) {
    getDataObject().setMail(pMail);
  }

  /**
   * @return all roles of the user
   */
  public Set<USER_ROLE> getRoles() {
    return getDataObject().getRoles();
  }

  /**
   * sets the roles
   * 
   * @param pRoles
   */
  public void setRoles(Set<USER_ROLE> pRoles) {
    getDataObject().setRoles(pRoles);
  }

  /**
   * @return all interests (ActionCategories) of the user
   */
  public List<DBRef<MongoActionCategory, String>> getInterests() {
    return actionInterestRefs;
  }

  /**
   * Sets the interests (ActionCategories)
   * 
   * @param pInterests
   */
  public void setInterests(List<DBRef<MongoActionCategory, String>> pInterests) {
    actionInterestRefs = pInterests;
    for (DBRef<MongoActionCategory, String> dbRef : pInterests) {
      MongoActionCategory fetched = dbRef.fetch();
      if (fetched != null)
        getDataObject().addInterest(fetched.getDataObject());
    }

  }

  public List<DBRef<MongoEventCategory, String>> getInterestedIn() {
    return eventInterestRefs;
  }

  /**
   * Sets the events the user is interested in
   * 
   * @param pInterestedIn
   */
  public void setInterestedIn(
      List<DBRef<MongoEventCategory, String>> pInterestedIn) {
    eventInterestRefs = pInterestedIn;
    for (DBRef<MongoEventCategory, String> dbRef : pInterestedIn) {
      MongoEventCategory fetched = dbRef.fetch();
      if (fetched != null)
        getDataObject().addInterestedIn(fetched.getDataObject());
    }
  }

  public List<MongoActionApplication> getActionApplications() {
    return actionApplicationsRefs;
  }

  /**
   * Sets the actionapplications of the user
   * 
   * @param pActionApplications
   */
  public void setActionApplications(
      List<MongoActionApplication> pActionApplications) {
    Set<ActionApplication> actions = new HashSet<ActionApplication>();
    for (MongoActionApplication dbRef : pActionApplications) {
      ActionApplication application = dbRef.getDataObject();
      actions.add(application);
    }
    getDataObject().setActionApplications(actions);
  }
}
